#include "timer.h"
#include "cot_key.h"

typedef enum
{
    KEY_0 = 0,
    KEY_1,
    KEY_UP,
    
    /* 勿删除,用来统计KEY的数目 */
    KEY_MAX_NUM
} KeyType_e;

static void InitKeyIo(void)
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟

	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
 	GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4

	//初始化 WK_UP-->GPIOA.0	  下拉输入
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入，默认下拉	  
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
}

cotKeyState_e ReadKey0(void)
{
	return (cotKeyState_e)!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);//读取按键
}

cotKeyState_e ReadKey1(void)
{
    return (cotKeyState_e)!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3);//读取按键
}

cotKeyState_e ReadKeyUp(void)
{
    return (cotKeyState_e)GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);//读取按键
}


void FML_KEY_Init(void)
{
    static cotKeyCfg_t s_keyTable[KEY_MAX_NUM] = {
        {.pfnKeyState = ReadKey0},
        {.pfnKeyState = ReadKey1},
        {.pfnKeyState = ReadKeyUp},
    };

    InitKeyIo();
    cotKey_Init(s_keyTable, KEY_MAX_NUM);
}



static void OnPhotoKeyFunCB(cotKey_t key)
{
    if (cotKey_IsTriggerWayMet(KEY_0, COT_KEY_TRIGGER_WAY_LOSSEN))  // 配置为双边触发，但只需要判断松开触发
    {
        cotMenu_Exit(false);
    }
}

static void OnPhotoLongPressModeKeyFunCB(cotKey_t key)
{
    static uint16_t s_cnt = 0;
    cotOled_SetText(35, 25, "LongMode", 0, FONT_16X16);
    
    if (!cotKey_IsLongPress(key))  // 判断是否触发了长按
    {
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS))
        {
            cotOled_SetText(40, 45, "s p", 0, FONT_16X16);  // 按下首次按下触发
        }

        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN))
        {
            cotOled_SetText(40, 45, "s l", 0, FONT_16X16);  // 识别到短按
        }
    }
    else
    {
        s_cnt++; // 识别到了长按后每1秒触发一次回调
        
        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_PRESS))
        {
            cotOled_SetText(40, 45, "l p", 0, FONT_16X16);  // 按下后触发了长按，即识别到了长按
        }

        if (cotKey_IsTriggerWayMet(key, COT_KEY_TRIGGER_WAY_LOSSEN))
        {
            s_cnt = 0;
            cotOled_SetText(40, 45, "l l", 0, FONT_16X16);   // 长按松开
        }
    }

    cotOled_SetIntegerNum(80, 45, s_cnt, 3, 0, FONT_16X16);  
}

static void OnPhotoClickModeKeyFunCB(cotKey_t key)
{
    cotOled_SetText(35, 25, "ClickCnt", 0, FONT_16X16);
    cotOled_SetText(40, 45, "   ", 0, FONT_16X16);
    cotOled_SetIntegerNum(80, 45, cotKey_GetClickCount(key), 3, 0, FONT_16X16);
}

uint32_t GetTimerMs(void)
{
    timeval_t ts;
    ReadSystemTime(&ts);
    return (ts.tv_msec);
}

int main(void)
{
    FML_KEY_Init();
	
    cotKey_Listen(KEY_0, OnPhotoKeyFunCB);
    cotKey_Listen(KEY_1, OnPhotoLongPressModeKeyFunCB);
    cotKey_Listen(KEY_UP, OnPhotoClickModeKeyFunCB);
    
    // cotKey_ConfigNormalMode(KEY_0, COT_KEY_TRIGGER_WAY_PRESS | COT_KEY_TRIGGER_WAY_LOSSEN, 0);
    cotKey_ResetDefaultConfig(KEY_0);  // 恢复默认配置，及普通双边触发模式（按下触发/松开触发）
    cotKey_ConfigLongPressMode(KEY_1, 2000, 1000);  // 区分长按和短按的时间为两秒，长按不松开则每1秒触发一次回调函数
    cotKey_ConfigClickMode(KEY_UP, 200);  // 以200毫秒为连击时间间隔 
	
    while (1)
    {
        if (g_20ms)
        {
            g_20ms = 0;
            cotKey_Scan(GetTimerMs());
        }
    }
}
